/**********************************/
/* Create a sample of individual observations
for treatment with STATA */
/**********************************/
/************************************************************/
libname corresp "C:\Users\Public\Documents\Entrees\20160704_01\Entree_CASD160630";
libname dads "\\casd-src4\Projets\AJUSTSA\Data\DADS_Panel DADS_2007";

/************************************************************/
/* construct data */
data table;
set dads.fichseq07 
/* select variables */
(keep = 
/* pays de naissance */
depnai
/* localisation: commune, d�partement de travail et de r�sidence */
dept comt depr comr
/* ann�e */
an 
/* industries */
apet apen cs2 cs2_anc
/* hommes */
sx
/* tps de travail */
ce 
/* SALAIRES */
/* salaire basecsg en euros 2007 *
bascsg bascsgr
/* salaire net en euros 2007, (estim� avant 1993?) *
netnet netnetr
/* salaire net fiscal / en euros courant */
sn /*snr 
/* avantages en nature / en euros constants *
av avr
/********************************************/
/* age */
age 
/* ann�e de validit� */
an
/* date d'entr�e dans le panel */
entpan 
/* date d'entr�e dans l'entreprise */
entsir
/* d�but et fin de r�mun�ration 
debremu finremu */
/* dur�e de la paye */
dp
/* statut 
ptt � partir de 93; fph � partir de 84; admin � partir de 88 ppalt */
st ptt domempl fph admin catjur
/* informations sur l'entreprise: nb de salari�s 
entreprise/�tablissements */
nbsa_ent nbsa_et
/* individual identifier */
nninouv
/* nir fictifs */
nnifict
/* siren */
sir sirfict /*pseudosir /* seulement 4 chiffres pour le nic*/ nic4
);
rename sx=homme;
rename dept=dep;
public=admin+fph+ptt;
/* Sub sample to debug
y=rand('BERNOUILLI',0.05);
if y=0 then delete;
drop y;
*/
run;

/* temps de travail */
data table;
set table;
complet=(ce="C");
run;

/* �limine les nirs fictifs */

/* Selection : �limine administration publique, 
ptt, edf, interim, partiel, 
non full year*/

/*******************
data table;
set table;
/* YEAR of entry in Firm/siren *
asir=substr(put(entsir,$4.),1,4);
run;
*/
/******************************************************/
/* NORMALIZE INDUSTRIAL CLASSIFICATION SYSTEMS: NAP40 */
/******************************************************/
/*proc freq data=table;
table apet apen;
run;*/
/* Apet: NAP jusqu'en 1992, NAF apr�s 1993, (LES DEUX EN 1993? Non!) */
data tableSM;
set table (keep = sir an nninouv apet apen cs2 cs2_anc) ;
if missing(apet) then apet=apen;
if an>1992 then naf=apet;
run;
/******************************/
/* Passage de Naf Rev 2 � NAF */
/******************************/
data tablesm;
set tablesm;
if naf="271Y"	then naf="271Z";
if naf="282C"	then naf="282A";
if naf="287Q"	then naf="287M";
if naf="291B"	then naf="291C";
if naf="291E"	then naf="291C";
if naf="292L"	then naf="292K";
if naf="292M"	then naf="292K";
if naf="295B"	then naf="295C";
if naf="295D"	then naf="295P";
if naf="295Q"	then naf="295P";
if naf="295R"	then naf="295P";
if naf="321C"	then naf="321B";
if naf="321D"	then naf="321B";
if naf="401A"	then naf="401Z";
if naf="401C"	then naf="401Z";
if naf="401E"	then naf="401Z";
if naf="402A"	then naf="401Z";
if naf="402C"	then naf="401Z";
if naf="518A"	then naf="516A";
if naf="518C"	then naf="516C";
if naf="518E"	then naf="516E";
if naf="518G"	then naf="516G";
if naf="518H"	then naf="516G";
if naf="518J"	then naf="516J";
if naf="518L"	then naf="516J";
if naf="518M"	then naf="516K";
if naf="518N"	then naf="516L";
if naf="518P"	then naf="516N";
if naf="519A"	then naf="517Z";
if naf="519B"	then naf="517Z";
if naf="551E"	then naf="551D";
if naf="554C"	then naf="923H";
if naf="642C"	then naf="641A";
if naf="642D"	then naf="642B";
if naf="711A"	then naf="711Z";
if naf="711B"	then naf="711Z";
if naf="722A"	then naf="722Z";
if naf="722C"	then naf="722Z";
if naf="748H"	then naf="722Z";
if naf="900E"	then naf="900C";
if naf="900G"	then naf="900C";
if naf="922D"	then naf="922C";
if naf="922E"	then naf="922C";
if naf="922F"	then naf="922C";
if naf="923K"	then naf="923H";
run;

proc sort data=tablesm;
by naf;
run;
/*proc freq data=tablesm;
table naf;
run;*/
data tablesm;
merge tablesm (in = x) corresp.correspnafnap6;
by naf;
if x;
run;
/*data erreur;
set tablesm;
if an<1993 then delete;
if missing(apet) then delete;
if not(missing(nap6)) then delete;
run;
proc freq data=erreur;
table an naf;
run;*/
data tablesm;
set tablesm;
if an<1993 then nap6=apet ;
run;
/***************************/
/* Passage de NAP6 � NAP40 */
/***************************/
data tablesm;
set tablesm;
nap6_2d=nap6;
nap6=substr(nap6,1,2);
run;
Proc sort data=tablesm;
by nap6;
run;
data tablesm;
merge tablesm corresp.correspnap100nap40;
by nap6;
run;
data tablesm;
set tablesm;
drop nap6;
rename nap6_2d=NAP6;
data tablesm; 
set tablesm;
rename sect38=nap40;
run;

/***********/
/* Erreurs */
/***********/
data tablesm;
set tablesm;
if missing(nap40) then nap40="M";
run;

data tablesm;
set tablesm (keep = sir an nninouv nap40);
run;
proc sort data=tablesm;
by sir nninouv an;
run;
proc sort data=table;
by sir nninouv an;
run;
data table;
merge table (in = x) tablesm;
by sir nninouv an;
if x;
run;

/*proc freq data=table;
table nap40;
run;
proc freq data=table;
by an;
table nap40;
run;*/
/******************************/
/* Passage Eventuel au NAP 20 */
/******************************/
/*****************/
/* NORMALIZE CSP */
/*****************/
data table;
set table;
/* redefine cs1 and cs1_anc because apprentis are missing in cs1 */
cs1=substr(put(cs2,$1.),1,1);
cs1_anc=substr(put(cs2_anc,$1.),1,1);
cs2b=substr(put(cs2,$2.),1,2);
cs2_ancb=substr(put(cs2_anc,$2.),1,2);
/* cs2h => cs2 harmonis�e */
cs2h=cs2;
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="31" then cs2h="37";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="32"	then cs2h="34";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="33"	then cs2h="38";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="34"	then cs2h="37";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="40"	then cs2h="46";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="41"	then cs2h="42";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="42"	then cs2h="43";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="43"	then cs2h="47";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="44"	then cs2h="46";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="51"	then cs2h="54";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="52"	then cs2h="54";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="53"	then cs2h="55";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="54" then cs2h="54";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="57" then cs2h="54";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="59" then cs2h="54";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="60" then cs2h="62";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="61" then cs2h="62";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="63" then cs2h="67";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="65" then cs2h="67";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="66" then cs2h="69";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="67" then cs2h="67";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="68" then cs2h="67";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="69" then cs2h="67";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="72" then cs2h="56";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="80" then cs2h="56";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="81" then cs2h="52";
if missing(cs2h) and not(missing(cs2_anc)) and cs2_anc="82" then cs2h="53";
run;

proc freq data=table;
table cs2h cs2_anc cs2;
run;

data table;
set table;
/* ancienne classification */
if cs1_anc="3" or cs1_anc="4" then csp="cad";
if cs1_anc="5" or cs1_anc="8" then csp="tec";
if cs1_anc="6" or cs1_anc="7" then csp="ouv";
/* nouvelle classification */
if cs1="3" or cs1="2" then csp="cad";
if cs1="5" or cs1="4" or cs1="1" then csp="tec";
/* cs1= 7 APPRENTIS STAGIAIRES */
if cs1="6" or cs1="7" then csp="ouv";
drop cs1 cs1_anc;
if missing(csp) then csp="ouv";
drop cs2b cs2_ancb;
run;
/***************/
/* Export Data */
/* SAS EXPORT  */
/***************/
/* selection des variables */
data tabm;
set table (keep = sir nic4 sirfict dp sn entsir entpan nninouv 
nap40 csp dep age an complet cs2 cs2_anc cs2h comt depr comr nbsa_ent nbsa_et
depnai public homme);
if homme=0 then delete;
run;
data tabw;
set table (keep = sir nic4 sirfict dp sn entsir entpan nninouv 
nap40 csp dep age an complet cs2 cs2_anc cs2h comt depr comr nbsa_ent nbsa_et
depnai public homme);
if homme=1 then delete;
run;
/* �chantillon global */
/* men */
proc export data=tabm  outfile="C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\indiv_dispm.dta"
replace dbms=dta;
run;
/* women */
proc export data=tabw  outfile="C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\indiv_dispw.dta"
replace dbms=dta;
run;
